<?php

namespace App\Services;

use App\Exceptions\InternalException;
use App\Models\Purchase;
use App\Models\PurchaseDetail;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class PurchaseService
{
    public function get($title)
    {
        $sql = Purchase::with('approval')->when($title, function ($query, $title) {
            return $query->where(function($query) use ($title) {
                $query->where('name','like', '%'.$title.'%');
            });
        });
        return $sql;
    }


    public function insert($input,$details){
        if($input['delivery_date']){
            $input['delivery_date'] = Date("Y-m-d",$input['delivery_date'] / 1000);
        }
        if($input['acceptance_photos']){
            $input['acceptance_photos'] = json_encode($input['acceptance_photos']);
        }
        if($input['delivery_note_photo']){
            $input['delivery_note_photo'] = json_encode($input['delivery_note_photo']);
        }
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }
        try {
            $data = DB::transaction(function () use ($input,$details,$res) {
                $data = Purchase::create($res);
                foreach ($details as $k => $v) {
                    $details[$k]['purchase_id'] = $data->id;
                    $details[$k]['company_id'] = $data->company_id;
                    $details[$k]['project_id'] = $data->project_id;
                    $details[$k]['admin_id'] = $data->admin_id;
                }
                PurchaseDetail::insert($details);
                if($data){
                    return $data->id;
                }
            });

            return $data;

        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function update($input,$details){
        if($input['delivery_date']){
            $input['delivery_date'] = Date("Y-m-d",$input['delivery_date'] / 1000);
        }
        if($input['acceptance_photos']){
            $input['acceptance_photos'] = json_encode($input['acceptance_photos']);
        }
        if($input['delivery_note_photo']){
            $input['delivery_note_photo'] = json_encode($input['delivery_note_photo']);
        }
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }
        unset($res['id']);
        try {
            $data = DB::transaction(function () use ($input,$details,$res) {
                $data = Purchase::where('id',$input['id'])->update($res);
                PurchaseDetail::where('purchase_id',$input['id'])->delete();
                foreach ($details as $k => $v) {
                    $details[$k]['purchase_id'] = $input['id'];
                    $details[$k]['company_id'] = $res['company_id'];
                    $details[$k]['project_id'] = $res['project_id'];
                    $details[$k]['admin_id'] = $res['admin_id'];
                }
                PurchaseDetail::insert($details);
                if($data){
                    return $input['id'];
                }
            });

            return $data;
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }
}